import { dataBase } from '~/server/utils/DbUtils'
import { readUser } from '~/server/utils/readUser'

export default defineEventHandler(async (event) => {
    // 接收用户信息
    const user = await readUser(event)
    if (user.errMsg !== undefined) {
        return user
    }
    // 查询数据库
    const result = dataBase.getUserByName(user.name)
    // 判断用户是否存在
    if (!result) {
        return {
            timestamp: Date.now(),
            errMsg: '用户不存在'
        }
    }
    // 判断密码是否正确
    if (result.password !== user.password) {
        return {
            timestamp: Date.now(),
            errMsg: '用户名或密码错误'
        }
    }
    // 不能把密码返回给用户
    return {
        timestamp: Date.now(),
        data: {
            name: result.name,
            email: result.email
        }
    }
})
